local shanren = fk.CreateSkill {
  name = "lingling__shanren",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable{
  ["lingling__shanren"] = "山人",
  [":lingling__shanren"] = "锁定技，其他角色于摸牌阶段外获得的牌对你无效。",

  ["@@lingling__shanren"] = "山人",
}

shanren:addLoseEffect(function(self, player, is_death)
  local room = player.room
  local mark = player:getTableMark("lingling__shanren_record")
  room:setPlayerMark(player, "lingling__shanren_record", 0)
  for _, id in ipairs(mark) do
    local card = Fk:getCardById(id)
    if card:getMark("@@lingling__shanren") > 0 and
      table.every(room.alive_players, function (p)
        return not table.contains(p:getTableMark("lingling__shanren_record"), id)
      end) then
      room:setCardMark(card, "@@lingling__shanren", 0)
    end
  end
end)

shanren:addAcquireEffect(function(self, player, is_start)
  if is_start then return end
  local room = player.room
  room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
    local phase = e:findParent(GameEvent.Phase)
    if phase and phase.data.phase == Player.Draw then
      return false
    end
    for _, move in ipairs(e.data) do
      if move.toArea == Player.Hand then
        for _, info in ipairs(move.moveInfo) do
          local id = info.cardId
          local card = Fk:getCardById(id)
          if card:getMark("@@lingling__shanren") == 0 and room:getCardArea(id) == Card.PlayerHand then
            room:setCardMark(card, "@@lingling__shanren", 1)
            room:addTableMark(player, "lingling__shanren_record", id)
          end
        end
      end
    end
  end, 0)
end)

shanren:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(shanren.name, true) then
      local toMarked = {}
      for _, move in ipairs(data) do
        local current = move.to
        if current and current.phase ~= Player.Draw and current ~= player and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(current:getCardIds("h"), info.cardId) then
              table.insert(toMarked, info.cardId)
            end
          end
        end
      end
      if #toMarked > 0 then
        event:setCostData(self, {cards = toMarked})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toMarked = table.simpleClone(event:getCostData(self).cards)
    local mark = player:getTableMark("lingling__shanren_record")
    table.insertTableIfNeed(mark, toMarked)
    room:setPlayerMark(player, "lingling__shanren_record", mark)
    for _, id in ipairs(toMarked) do
      room:setCardMark(Fk:getCardById(id), "@@lingling__shanren", 1)
    end
  end,

  can_refresh = function(self, event, target, player, data)
    local remove = {}
    for _, move in ipairs(data) do
      if move.from == nil or move.toArea ~= Card.Processing then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card:getMark("@@lingling__shanren") ~= 0 and table.contains({ Card.Processing, Card.PlayerHand }, info.fromArea) then
            table.insert(remove, info.cardId)
          end
        end
      end
    end
    if #remove > 0 then
      event:setCostData(self, {cards = remove})
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local remove = table.simpleClone(event:getCostData(self).cards)
    local mark = player:getTableMark("lingling__shanren_record")
    for _, id in ipairs(remove) do
      room:setCardMark(Fk:getCardById(id), "@@lingling__shanren", 0)
      table.removeOne(mark, id)
    end
    room:setPlayerMark(player, "lingling__shanren_record", mark)
  end
})
shanren:addEffect(fk.PreCardEffect, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(shanren.name) and data.card:getMark("@@lingling__shanren") ~= 0 and data.to == player
  end,
  on_use = function(self, event, target, player, data)
    data.nullified = true
  end,
})

return shanren
